package com.ruoyi.web.controller.teaching;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.teaching.domain.TeachingClasses;
import com.ruoyi.teaching.domain.TeachingCourse;
import com.ruoyi.teaching.service.ITeachingCourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.TeachingCourseFolder;
import com.ruoyi.teaching.service.ITeachingCourseFolderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 课程目录Controller
 *
 * @author William
 * @date 2022-10-11
 */
@Controller
@RequestMapping("/teaching/folder")
public class TeachingCourseFolderController extends BaseController {
    private String prefix = "teaching/folder";

    @Autowired
    private ITeachingCourseFolderService teachingCourseFolderService;
    @Autowired
    private ITeachingCourseService courseService;

    @RequiresPermissions("teaching:folder:view")
    @GetMapping()
    public String folder(@RequestParam("courseId") Long courseId, ModelMap mmap) {
        TeachingCourse course = courseService.selectTeachingCourseByCourseId(courseId);
        mmap.put("course", course);
        return prefix + "/folder";
    }

    /**
     * 查询课程目录树列表
     */
    @RequiresPermissions("teaching:folder:list")
    @PostMapping("/list")
    @ResponseBody
    public List<TeachingCourseFolder> list(TeachingCourseFolder teachingCourseFolder) {
        List<TeachingCourseFolder> list = teachingCourseFolderService.selectTeachingCourseFolderList(teachingCourseFolder);
        return list;
    }

    /**
     * 导出课程目录列表
     */
    @RequiresPermissions("teaching:folder:export")
    @Log(title = "课程目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingCourseFolder teachingCourseFolder) {
        List<TeachingCourseFolder> list = teachingCourseFolderService.selectTeachingCourseFolderList(teachingCourseFolder);
        ExcelUtil<TeachingCourseFolder> util = new ExcelUtil<TeachingCourseFolder>(TeachingCourseFolder.class);
        return util.exportExcel(list, "课程目录数据");
    }

    /**
     * 新增课程目录
     */
    @GetMapping(value = {"/add/{folderId}", "/add/"})
    public String add(@PathVariable(value = "folderId", required = false) Long folderId, @RequestParam("courseId") Long courseId, ModelMap mmap) {
        if (StringUtils.isNotNull(folderId)) {
            mmap.put("teachingCourseFolder", teachingCourseFolderService.selectTeachingCourseFolderByFolderId(folderId));
        }
        if (StringUtils.isNotNull(courseId)) {
            mmap.put("course", courseService.selectTeachingCourseByCourseId(courseId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存课程目录
     */
    @RequiresPermissions("teaching:folder:add")
    @Log(title = "课程目录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingCourseFolder teachingCourseFolder) {


        int s = teachingCourseFolderService.insertTeachingCourseFolder(teachingCourseFolder);

        return toAjax(s);
    }

    /**
     * 修改课程目录
     */
    @RequiresPermissions("teaching:folder:edit")
    @GetMapping("/edit/{folderId}")
    public String edit(@PathVariable("folderId") Long folderId, ModelMap mmap) {
        TeachingCourseFolder teachingCourseFolder = teachingCourseFolderService.selectTeachingCourseFolderByFolderId(folderId);
        mmap.put("teachingCourseFolder", teachingCourseFolder);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程目录
     */
    @RequiresPermissions("teaching:folder:edit")
    @Log(title = "课程目录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingCourseFolder teachingCourseFolder) {
        return toAjax(teachingCourseFolderService.updateTeachingCourseFolder(teachingCourseFolder));
    }

    /**
     * 删除
     */
    @RequiresPermissions("teaching:folder:remove")
    @Log(title = "课程目录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{folderId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("folderId") Long folderId) {
        return toAjax(teachingCourseFolderService.deleteTeachingCourseFolderByFolderId(folderId));
    }

    /**
     * 选择课程目录树
     */
    @GetMapping(value = {"/selectFolderTree/{folderId}", "/selectFolderTree/"})
    public String selectFolderTree(@PathVariable(value = "folderId", required = false) Long folderId, @RequestParam("courseId") Long courseId, ModelMap mmap) {
        if (StringUtils.isNotNull(folderId)) {
            mmap.put("teachingCourseFolder", teachingCourseFolderService.selectTeachingCourseFolderByFolderId(folderId));
        }
        if (StringUtils.isNotNull(courseId)) {
            mmap.put("course", courseService.selectTeachingCourseByCourseId(courseId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载课程目录树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(@RequestParam("courseId") Long courseId) {
        TeachingCourseFolder folder = new TeachingCourseFolder();
        if (StringUtils.isNotNull(courseId)) {
            folder.setCourseId(courseId);
        }
        List<Ztree> ztrees = teachingCourseFolderService.selectTeachingCourseFolderTree(folder);

        return ztrees;
    }


}
